package io.renren.modules.sys.service.impl;

import io.renren.modules.sys.dao.CarTypeMapper;
import io.renren.modules.sys.entity.CarType;
import io.renren.modules.sys.service.CarTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class CarTypeServiceImpl implements CarTypeService {
    @Autowired
    private CarTypeMapper carTypeMapper;

    @Override
    public List<CarType> selAllType() {

        List<CarType> carTypeList = carTypeMapper.selAllType();
        List<CarType> collect = carTypeList.stream().filter(i ->
               Integer.valueOf(i.getParentId())  == 0
        ).map(menu -> {
            menu.setChildren(getChidrens(menu,carTypeList));
            return menu;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public void save(CarType carType) {
        carTypeMapper.save(carType);
    }

    @Override
    public void delete(long id) {
        carTypeMapper.delete(id);
    }

    @Override
    public void update(CarType carType) {
        carTypeMapper.update(carType);
    }

    public List<CarType> getChidrens(CarType root,List<CarType> all){
        List<CarType> children  = all.stream().filter(carType -> {
            return Integer.valueOf(carType.getParentId()) == root.getId();
        }).map(carType -> {
            carType.setChildren(getChidrens(carType,all));
            return carType;
        }).collect(Collectors.toList());
        return children;
    }

}
